package org.wenhua.yuan.and.views.tab;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Typeface;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;

import org.wenhua.yuan.R;
import org.wenhua.yuan.and.util.DisplayConvert;

/**底部导航按钮 - 单个tab*/
public class TabBtnBottom extends RelativeLayout implements ITab<TabBtnBottomInfo<?>> {

    private TabBtnBottomInfo<?> tabBtnBottomInfo;
    private ImageView tabImage;
    private TextView tabIcon;
    private TextView tabName;

    public TabBtnBottom(Context context) {
        this(context, null);
    }

    public TabBtnBottom(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public TabBtnBottom(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initTabBtnBottom();
    }


    /**设置底部导航按钮数据实体
     * 并初始化5个tab的默认显示tab状态
     * */
    @Override
    public void setTabInfo(TabBtnBottomInfo<?> data) {
        this.tabBtnBottomInfo = data;
        inflateBtnBottomInfo(false, true);
    }

    private void inflateBtnBottomInfo(boolean isSelected, boolean isFirstInit) {
        if (tabImage == null) return;
        if (this.tabBtnBottomInfo.tabType == TabBtnBottomInfo.TabType.BITMAP) {
            if (isFirstInit) {
                tabImage.setVisibility(View.VISIBLE);
                tabIcon.setVisibility(View.GONE);
                tabName.setVisibility(View.VISIBLE);
                tabName.setText(this.tabBtnBottomInfo.name);

            }
            if (isSelected) {
                tabImage.setImageBitmap(this.tabBtnBottomInfo.selectedBitmap);
                tabName.setTextColor(getTextColor(this.tabBtnBottomInfo.tintColor));
            } else {
                tabImage.setImageBitmap(this.tabBtnBottomInfo.defaultBitmap);
                tabName.setTextColor(getTextColor(this.tabBtnBottomInfo.defaultColor));
            }


        } else if (this.tabBtnBottomInfo.tabType == TabBtnBottomInfo.TabType.ICON) {
            if (isFirstInit) {
                tabImage.setVisibility(View.GONE);
                tabIcon.setVisibility(View.VISIBLE);
                Typeface typeface = Typeface.createFromAsset(getContext().getAssets(), this.tabBtnBottomInfo.iconFont);
                if (typeface != null)
                    tabIcon.setTypeface(typeface);
                tabName.setText(this.tabBtnBottomInfo.name);
            }

            if (isSelected) {
                String iconName = TextUtils.isEmpty(this.tabBtnBottomInfo.selectedIconName)?this.tabBtnBottomInfo.defaultIconName:this.tabBtnBottomInfo.selectedIconName;
                tabIcon.setText(iconName);
                tabIcon.setTextColor(getTextColor(this.tabBtnBottomInfo.tintColor));
                tabName.setTextColor(getTextColor(this.tabBtnBottomInfo.tintColor));
            } else {
                tabIcon.setText(this.tabBtnBottomInfo.defaultIconName);
                tabIcon.setTextColor(getTextColor(this.tabBtnBottomInfo.defaultColor));
                tabName.setTextColor(getTextColor(this.tabBtnBottomInfo.defaultColor));
            }


        }
    }

    private int getTextColor(Object tintColor) {
        if (tintColor instanceof String) {
            return Color.parseColor((String) tintColor);
        } else {
            return (int)tintColor;
        }

    }

    /**设置底部导航按钮高度*/
    @Override
    public void resizeHeight(int height) {
        ViewGroup.LayoutParams params = getLayoutParams();
        params.height = DisplayConvert.dp2px(height, getResources());
        setLayoutParams(params);
        tabName.setVisibility(INVISIBLE);
    }

    /** 初始化底部导航按钮,并将按钮布局添加到当前TabBtnBottom布局中 */
    private void initTabBtnBottom () {
        LayoutInflater.from(getContext()).inflate(R.layout.tab_btn_bottom, this);
        tabImage = findViewById(R.id._tabImg);
        tabIcon = findViewById(R.id._textIcon);
        tabName = findViewById(R.id._textName);
    }


    /**获取按钮info*/
    public TabBtnBottomInfo<?> getTabBtnBottomInfo() {
        return tabBtnBottomInfo;
    }


    /** 对当前单个tab设置监听的监听方法，目的是监听点击事件，然后显示被选中状态。*/
    @Override
    public void onTabSelectedListener(int index, @NonNull TabBtnBottomInfo<?> curTabInfo, @NonNull TabBtnBottomInfo<?> nextTabInfo) {
        // 点击了同一个tab或没有点击到当前导航栏上的tab按钮
        if (curTabInfo == nextTabInfo || this.tabBtnBottomInfo != curTabInfo && this.tabBtnBottomInfo != nextTabInfo) {
            return;
        }

        // 如果点击了新的按钮
        if (this.tabBtnBottomInfo == nextTabInfo) {
            inflateBtnBottomInfo(true, false);
        } else {
            inflateBtnBottomInfo(false, false);
        }
    }
}
